package com.appspot.thinkhea;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Picture;
import com.appspot.thinkhea.view.SeleableGridRecordAdapter;

public class PanelRandomTeam extends Activity {
	private Vector<Picture> c;
	private MyDBHelper db;
	private GridView v;
	private String tag = "PanelRandomTeam";
	
	private int players =0;
	private int players_per_team =0;
	private int teams =0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_random_picture);
		setContentView(R.layout.panel_add_record);
		
		Bundle bundle = this.getIntent().getExtras();
		players = bundle.getInt("PLAYERS");
		players_per_team = bundle.getInt("PLAYER_PER_TEAM");
		teams = bundle.getInt("TEAM");
		
		// handle list event and model
		v = (GridView) findViewById(R.id.RecordGridView);
		v.setFocusable(true);
		v.setClickable(true);
		v.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				((SeleableGridRecordAdapter) a.getAdapter())
						.setSelected(position);
			}
		});
		loadPictures();

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(PanelRandomTeam.this,
						PanelSelectRandomFormat.class);
				PanelRandomTeam.this.startActivity(myIntent);

			}
		});

		// Add Button Listener
		Button ba = (Button) findViewById(R.id.BtnAddRecordNext);
		ba.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				GridView lv = (GridView) findViewById(R.id.RecordGridView);
				Vector<Picture> pics = ((SeleableGridRecordAdapter) lv
						.getAdapter()).getSelected();

				if (pics.size() != players) {
					Toast.makeText(PanelRandomTeam.this,
							"Please select "+players+" players for a game.",
							Toast.LENGTH_SHORT).show();
					return;
				}
				// save selected Game into DB
				long result = savePictures(pics);
				if (result == -1) {
					Toast.makeText(PanelRandomTeam.this,
							tag + " Error in saving record.",
							Toast.LENGTH_SHORT).show();
					return;
				}
				Toast.makeText(PanelRandomTeam.this, "Random Team up.",
						Toast.LENGTH_SHORT).show();
					
				Bundle bundle = new Bundle();
				bundle.putInt("PLAYERS", players);
				bundle.putInt("PLAYER_PER_TEAM", players_per_team);
				bundle.putInt("TEAM", teams);
				
				 Intent myIntent = new Intent(PanelRandomTeam.this,
						 PanelRandomTeamView.class);
				 myIntent.putExtras(bundle);
				 PanelRandomTeam.this.startActivity(myIntent);
			}
		});

	}

	protected long savePictures(Vector<Picture> pics) {
		if (db == null) {
			db = new MyDBHelper(this);
		}
		db.open();
		long result = -1;
		try {
			for (int b = 0; b < pics.size(); b++) {
				Picture f = pics.get(b);
				f.setRate(1);
				result = db.updatePicutre(f);
			}			
		} catch (SQLException e) {
			Log.e(tag, "SQLException - addRecord");
		}
		db.close();
		return result;
	}

	protected void loadPictures() {
		// load data from DB
		if (c != null) {
			c.clear();
		}
		if (db == null) {
			db = new MyDBHelper(this);
		}
		db.open();
		c = db.getAllPicutres();
		
		for (int b = 0; b < c.size(); b++) {
			Picture f = c.get(b);
			f.setRate(0);
			db.updatePicutre(f);
		}	
		db.close();
		SeleableGridRecordAdapter iAdapter = new SeleableGridRecordAdapter(
				this, c);
		// handle list event and model
		v.setAdapter(iAdapter);
	}
}